package com.example.managerment.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

@Component
public class RedisUtil {
    @Autowired
    RedisTemplate<String,String> stringRedisTemplate;

    @Autowired
    RedisTemplate<Object,Object> redisTemplate;

    /**
     * 缓存token
     * @param key
     * @param token
     */
    public void save(String key,String token){
        key += "_token";
        stringRedisTemplate.opsForValue().set(key, token);
    }

    /**
     * 缓存用户身份
     * @param key
     * @param authentication
     */
    public void save(String key, Authentication authentication){
        redisTemplate.opsForValue().set(key,authentication);
    }

    public String getToken(String key){
        key += "_token";
        return stringRedisTemplate.opsForValue().get(key);
    }

    public Authentication getAuthentication(String key){
        return (Authentication) redisTemplate.opsForValue().get(key);
    }

    public void delete(String key){
        redisTemplate.delete(key);
        stringRedisTemplate.delete(key);
    }




}
